
export default {

    EXFUNC_LIST: [
        "showWin",
        "showView",
        "closeView",
        "closeWin",
        "showToast",
        "chkTouchGUI"
    ],

    addToGUI(cont) {
        if (this._uiroot) {
            this._uiroot.add(cont);
        }
    },

    //扩展绑定
    bindRoot: function (sceneobj, uiroot) {
        this.target_ = sceneobj
        this._uiroot = uiroot;
        this._view = undefined
        this._win1 = undefined
        this._win2 = undefined
        this._alerttxt = undefined

        this._raycaster = new THREE.Raycaster();
        this._mouse_vec3 = new THREE.Vector3();

        //bind func
        var funcname
        var self = this
        for (var idx in this.EXFUNC_LIST) {
            funcname = this.EXFUNC_LIST[idx]
            if (!sceneobj[funcname]) {
                sceneobj[funcname] = (function () {
                    var _tmpfname = funcname
                    return function () {
                        return self[_tmpfname](arguments[0], arguments[1])
                    }
                })()
            }
        }
    },

    /**
     * 显示View
     *  应用禁止直接调用
     * @param viewLevel
     * @param {ViewBase}viewCls
     */
    showView: function (viewLevel, viewCls) {
        this.closeWin2();
        this.closeWin1();
        var args = Array.prototype.slice.call(arguments, 2);
        this._showView(viewLevel, viewCls, args);
    },

    showWin: function (winCls, winlv) {
        winlv = winlv || 1;
        let args = Array.prototype.slice.call(arguments);
        args.shift();

        return winlv == 1 ? this._showWin1(winCls, args) : shiw._showWin22(winCls, args);

    },

    /**
     * 显示Win1
     *  应用禁止直接调用
     * @param {WinBase}winCls
     */
    showWin1: function (winCls) {
        this.closeWin2();
        var args = Array.prototype.slice.call(arguments);
        args.shift();
        this._showWin1(winCls, args);
    },

    /**
     * 显示Win2
     *  应用禁止直接调用
     * @param {WinBase}winCls
     */
    showWin2: function (winCls) {
        var args = Array.prototype.slice.call(arguments);
        args.shift();
        this._showWin2(winCls, args);
    },

    /**
     * 显示AlertTxt
     *  应用禁止直接调用
     * @param {String}txt
     * @param {Number}[size]
     */
    showAlertTxt: function (txt, size) {
        this.closeAlertTxt();
    },

    /**
     * 关闭Win1
     *  应用禁止直接调用
     */
    closeWin1: function () {
        if (this._win1) this._win1.remove();
        this._win1 = null;
    },

    /**
     * 关闭Win2
     *  应用禁止直接调用
     */
    closeWin2: function () {
        if (this._win2) this._win2.remove();
        this._win2 = null;
    },

    closeWin: function () {
        this.closeWin1();
        this.closeWin2();
    },

    /**
     *清理view
     *
     */
    closeView: function () {
        if (this._view) {
            this._view.removeFromParent();
            this._view = undefined;
        }
    },

    /**
     * 关闭AlertTxt
     *  应用禁止直接调用
     */
    closeAlertTxt: function () {
        if (this._alerttxt) this._alerttxt.remove();
        this._alerttxt = null;
    },

    /**
     * 返回view
     *  应用禁止直接调用
     * @returns {ViewBase}
     */
    getView: function () {
        return this._view;
    },

    /**
     * 返回win1
     *  应用禁止直接调用
     * @returns {Win1Base}
     */
    getWin1: function () {
        return this._win1;
    },

    /**
     * 返回win2
     *  应用禁止直接调用
     * @returns {Win1Base}
     */
    getWin2: function () {
        return this._win2;
    },

    _showView: function (viewLevel, viewCls, args) {
        if (this._view) this._view.remove();
        this._view = new viewCls(args);
        this.addToGUI(this._view);
    },

    _showWin1: function (winCls, args) {
        if (this._win1) {
            this._win1.remove();
            this._win1 = null;
        }
        this._win1 = new winCls(args)
        this.addToGUI(this._win1)
        // , G.DEPTH.WIN1).pos(d.w / 2, d.h / 2 - 50);
    },

    _showWin2: function (winCls, args) {
        if (this._win2) {
            this._win2.remove();
            this._win2 = null;
        }
        this._win2 = new winCls(args)
        this.addToGUI(this._win2)
        // , G.DEPTH.WIN2).pos(d.w / 2, d.h / 2);
    },

    showToast: function (txt) {
        //tdoo
    },

    chkTouchGUI: function (evt, camera) {
        if (this._view) { 
            this._view.onTouch(evt, camera) 
            return true;
        }
        else{
            console.log("cover mask");
        };
    },

    isMask: function(){
        return (this._view != undefined || this._win1 != undefined);
    }
}